﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using SPPivotViewer.HttpHandler.Internal;
using System.Net;

namespace SPPivotViewer.HttpHandler
{
    public class SPCxmlHandler : IHttpHandler
    {
        
        public SPCxmlHandler()
        {
            
        }
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
             ServeCxml(context);
        }
        private void ServeCxml(HttpContext context)
        {
            try
            {
                SPListCollectionParam param = new SPListCollectionParam(context);
                Collection col = CollectionCache.Instance.Get(param.Key);
                string collectionFileName = SPHandlerHelper.GetUrlFileBody(context.Request.Url);
                if (col != null)
                {
                    ServeCxml(context, col, collectionFileName, param.Key);
                    return;
                }
                string strKey = context.Request.QueryString["Key"];
                if (!string.IsNullOrEmpty(strKey))
                {
                    Collection colCache = CollectionCache.Instance.Get(strKey);
                    if (colCache != null)
                    {
                        context.Response.ContentType = "text/xml";
                        colCache.ToCxml(context.Response.Output);
                        return;
                    }
                }
                Collection collection = null;
                if (collectionFileName == "UserProfileCollection")
                {
                    SPProfileUserCollectionFactory factory = new SPProfileUserCollectionFactory();

                    collection = factory.MakeCollection(
                        new CollectionRequestContext(context.Request.QueryString, context.Request.Url.AbsolutePath), collectionFileName, param);

                }
                else if (collectionFileName == "AllWebNavigationCollection")
                {
                    SPAllListCollectionFactory factory = new SPAllListCollectionFactory();

                    collection = factory.MakeCollection(
                        new CollectionRequestContext(context.Request.QueryString, context.Request.Url.AbsolutePath), collectionFileName);

                }
                else if (collectionFileName == "SearchPivot")
                {
                    SPSearchFactory factory = new SPSearchFactory();

                    collection = factory.MakeCollection(
                        new CollectionRequestContext(context.Request.QueryString, context.Request.Url.AbsolutePath), collectionFileName);

                }
                else if (collectionFileName == "SiteDataQueryFactory")
                {
                    SPSiteDataQueryFactory factory = new SPSiteDataQueryFactory();

                    collection = factory.MakeCollection(
                        new CollectionRequestContext(context.Request.QueryString, context.Request.Url.AbsolutePath), collectionFileName);

                }
                else
                {
                    SPListCollectionFactory factory = new SPListCollectionFactory();

                    collection = factory.MakeCollection(
                        new CollectionRequestContext(context.Request.QueryString, context.Request.Url.AbsolutePath), collectionFileName, param);

                }
                if (null == collection)
                {
                    context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                    context.Response.StatusDescription = "Collection is empty.";
                    return;
                }

                ServeCxml(context, collection, collectionFileName, param.Key);
            }
            catch (Exception ex)
            {
                SPPivotViewer.Utility.logger.ErrorMessage("CXML", ex.ToString());
            }
        }

        private void ServeCxml(HttpContext context, Collection collection, string collectionFileName,string key)
        {
            collection.SetDynamicDzc(collectionFileName,key);
            CollectionCache.Instance.Add(key, collection);

            context.Response.ContentType = "text/xml";
            context.Response.Write("<key>"+ key +"</key>");
        }
    }
}
